package com.cqc.compresshelper01;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.format.Formatter;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.liji.takephoto.TakePhoto;
import com.nanchen.compresshelper.CompressHelper;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private CompressHelper compressHelper;
    private File oldFile;
    private String selectPath = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        compressHelper = CompressHelper.getDefault(getApplicationContext());
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/c.png";
        oldFile = new File(path);//   /storage/emulated/0/abc.png


        btn1 = (Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File newFile = compressHelper.compressToFile(oldFile);
            }
        });


        btn2 = (Button) findViewById(R.id.btn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File file = new CompressHelper.Builder(MainActivity.this)
                        .setMaxWidth(500)
                        .setMaxHeight(300)
                        .setFileName("newName")//自动添加后缀名.jpeg
                        .setQuality(50)
                        .setDestinationDirectoryPath(Environment.getExternalStorageDirectory().getAbsolutePath())
                        .build()
                        .compressToFile(oldFile);
                Log.d(TAG, "file地址：" + file.getAbsolutePath());
            }
        });

        btn3 = (Button) findViewById(R.id.btn3);
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TakePhoto takePhoto = new TakePhoto(MainActivity.this);
                takePhoto.setOnPictureSelected(new TakePhoto.onPictureSelected() {
                    @Override
                    public void select(String path) {
                        selectPath = path;
                        Log.d(TAG, "选择的图片地址：" + path);
                    }
                });
                takePhoto.show();
            }
        });
    }
}
